#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/6 11:37
# @Author  : Crissu
# @Site    : 
# @File    : compute_reference_value.py
# @Software: PyCharm
import xlrd, xlwt
from xlutils.copy import copy
from ResultProcessing.configs.universalConfig import idx2classMap, class2idxMap

# 读 xls 文档，计算 TP \ FP \ FN \ P \ R \ F1
def readXLS(src):
    workbook = xlrd.open_workbook(src)  # 打开工作簿
    sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
    worksheet = workbook.sheet_by_name(sheets[0])  # 获取工作簿中所有表格中的的第一个表格
    # 计算参数
    TP = {}
    FP = {}
    FN = {}
    P = {}
    R = {}
    F1 = {}
    # 初始化 map
    for i in range(85):
        TP[i] = 0
        FP[i] = 0
        FN[i] = 0
        P[i] = 0.0
        R[i] = 0.0
        F1[i] = 0.0

    for i in range(1, worksheet.nrows):
        groundTruth = int(worksheet.cell_value(i, 1))
        predict = int(worksheet.cell_value(i, 2))
        if groundTruth == predict:
            TP[groundTruth] += 1
        else:
            FN[groundTruth] += 1
            FP[predict] += 1

    # 计算 P R
    for i in range(0, 85):
        P[i] = TP[i] / (TP[i] + FP[i])
        R[i] = TP[i] / (TP[i] + FN[i])

    # 计算F1
    for i in range(0, 85):
        F1[i] = (2 * P[i] * R[i]) / (P[i] + R[i])

    # 计算74类平均
    className_74 = ['46ban_feng_die', '48sui_ban_qing_feng_die', '116hei_mai_jia_die', '61xiao_hong_jia_die', '85mei_yan_jia_die', '91da_hong_jia_die', '168yu_feng_e', '152she_feng_die', '26dai_huang_feng', '42hei_dai_shi_ya_ying', '30hei_wei_ao_da_ye_chan', '81hei_wei_ye_chan', '32lv_zhong_si', '94duan_ban_you_cao_zhong', '123mian_huang', '109kong_zi_qian_qiao_jia', '172bian_qiao_jia', '146xing_tian_niu', '171guang_jian_xing_tian_niu', '104huang_xing_tian_niu', '163sang_tian_niu', '47zhong_hua_cao_zhong', '57ri_ben_tiao_zhong_si', '40dong_ya_fei_huang', '112bai_nong_die', '31ya_hui_die', '50lao_bao_jia_die', '55zhong_huan_jia_die', '137qu_wen_zhi_jia_die', '59si_dai_feng_die', '139yu_dai_feng_die', '70jin_shang_feng_die', '158chong_yang_mu_jin_ban_e', '150qing_feng_die', '49liu_li_jia_die', '87hei_nong_die', '60cui_lan_yan_jia_die', '54bai_dai_ao_jia_die', '79dao_mei_yan_die', '39zhi_wen_dao_nong_die', '178wu_jiu_da_can_e', '51gan_shu_la_gui_jia', '33lv_hui_die', '38an_mai_cai_fen_die', '21juan_fen_die', '63liu_li_hui_die', '141mei_guo_bai_e', '36ka_fei_tou_chi_tian_e', '44qing_bei_zhang_hui_tian_e', '100zhu_ma_shuang_ji_tian_niu', '45pu_tong_lou_gu', '35zi_jing_jia', '134zhong_hua_xi_shuai', '126hei_e_guang_ye_jia', '74shi_xing_piao_ying_ye_jia', '119dou_yan_jing', '71zhang_jian_ji_yuan_chun', '83hong_ji_zhang_chun', '77tou_ming_shu_guang_la_chan', '101huang_yang_juan_ye_ming', '1tu_si_zi', '11yang_ti', '14lv_cao', '24ping_che_qian', '25tun_cao', '22nie_li', '20bai_mao', '18sang_ji_sheng', '13luo_shi', '12xi_han_lian_zi_cao', '15ye_ge', '16wu_zhao_jin_long', '27feng_yan_lian', '2niu_qie_zi']
    className_22 = ['109kong_zi_qian_qiao_jia', '116hei_mai_jia_die', '123mian_huang', '146xing_tian_niu', '152she_feng_die', '168yu_feng_e', '171guang_jian_xing_tian_niu', '172bian_qiao_jia', '26dai_huang_feng', '30hei_wei_ao_da_ye_chan', '32lv_zhong_si', '40dong_ya_fei_huang', '42hei_dai_shi_ya_ying', '46ban_feng_die', '47zhong_hua_cao_zhong', '48sui_ban_qing_feng_die', '57ri_ben_tiao_zhong_si', '61xiao_hong_jia_die', '81hei_wei_ye_chan', '85mei_yan_jia_die', '91da_hong_jia_die', '94duan_ban_you_cao_zhong']

    PAverage_74 = 0.0
    RAverage_74 = 0.0
    F1Average_74 = 0.0
    PAverage_22 = 0.0
    RAverage_22 = 0.0
    F1Average_22 = 0.0

    className_74_list = list()
    precision_74_list = list()
    idx_74_list = list()
    className_22_list = list()
    precision_22_list = list()
    idx_22_list = list()

    for idx in range(85):
        if idx2classMap[idx] in className_74:
            className_74_list.append(idx2classMap[idx])
            precision_74_list.append(P[idx])
            idx_74_list.append(idx)
            PAverage_74 += P[idx]
            RAverage_74 += R[idx]
            F1Average_74 += F1[idx]
        if idx2classMap[idx] in className_22:
            className_22_list.append(idx2classMap[idx])
            precision_22_list.append(P[idx])
            idx_22_list.append(idx)
            PAverage_22 += P[idx]
            RAverage_22 += R[idx]
            F1Average_22 += F1[idx]

    PAverage_74 = PAverage_74 / 74
    RAverage_74 = RAverage_74 / 74
    F1Average_74 = F1Average_74 / 74
    PAverage_22 = PAverage_22 / 22
    RAverage_22 = RAverage_22 / 22
    F1Average_22 = F1Average_22 / 22
    print("PAverage_74:", PAverage_74)
    print("RAverage_74:", RAverage_74)
    print("F1Average_74:", F1Average_74)
    print("PAverage_22:", PAverage_22)
    print("RAverage_22:", RAverage_22)
    print("F1Average_22:", F1Average_22)
    print("idx_74_list:", idx_74_list)
    print("className_74_list:", className_74_list)
    print("precision_74_list:", precision_74_list)
    print("idx_22_list:", idx_22_list)
    print("className_22_list:", className_22_list)
    print("precision_22_list:", precision_22_list)

    class_74_list = list()
    class_74_list.append(idx_74_list)
    class_74_list.append(className_74_list)
    class_74_list.append(precision_74_list)
    class_22_list = list()
    class_22_list.append(idx_22_list)
    class_22_list.append(className_22_list)
    class_22_list.append(precision_22_list)
    # 写入 xls
    xls_path = "./new_result.xls"
    write_excel_xls(xls_path, "class_74", class_74_list)
    write_excel_xls_append(xls_path, class_22_list)

# 写 xls
def write_excel_xls(path, sheet_name, value):
    row = len(value[0])  # 获取需要写入数据的行数
    col = len(value)
    workbook = xlwt.Workbook()  # 新建一个工作簿
    sheet = workbook.add_sheet(sheet_name)  # 在工作簿中新建一个表格
    for i in range(0, row):
        for j in range(0, col):
            sheet.write(i, j, value[j][i])  # 像表格中写入数据（对应的行和列）
    workbook.save(path)  # 保存工作簿
    print("xls格式表格写入数据成功！")

# 追加 xls
def write_excel_xls_append(path, value):
    row = len(value[0])  # 获取需要写入数据的行数
    col = len(value)
    workbook = xlrd.open_workbook(path)  # 打开工作簿
    sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
    worksheet = workbook.sheet_by_name(sheets[0])  # 获取工作簿中所有表格中的的第一个表格
    rows_old = worksheet.nrows  # 获取表格中已存在的数据的行数
    rows_old += 5
    new_workbook = copy(workbook)  # 将xlrd对象拷贝转化为xlwt对象
    new_worksheet = new_workbook.get_sheet(0)  # 获取转化后工作簿中的第一个表格
    for i in range(0, row):
        for j in range(0, col):
            new_worksheet.write(i+rows_old, j, value[j][i])  # 追加写入数据，注意是从i+rows_old行开始写入
    new_workbook.save(path)  # 保存工作簿
    print("xls格式表格【追加】写入数据成功！")

# 测试代码
def test():
    src = './test.xls'
    workbook = xlrd.open_workbook(src)  # 打开工作簿
    sheets = workbook.sheet_names()  # 获取工作簿中的所有表格
    worksheet = workbook.sheet_by_name(sheets[0])  # 获取工作簿中所有表格中的的第一个表格
    m = {}
    mm = {}
    class_74 = list()
    for i in range(0, worksheet.nrows):
        idx = int(worksheet.cell_value(i, 0))
        name = str(worksheet.cell_value(i, 2))
        class_74.append(name)
        if idx2classMap[idx] != name:
            m[idx] = name
            mm[idx] = idx2classMap[idx]
    print(m)
    print(mm)
    print(class_74)
    print(len(class_74))


if __name__ == '__main__':
    srcPath = './dbt-resnext50_result_gt.xls'
    readXLS(srcPath)
    # test()
    # print(idx2classMap[21])









